"""
Case Type   : gms_output
Case Name   : 创建gms_output插件
Create At   : 2024/08/06
Owner       : zbanbiao.chen
Description :
    1、A库下创建gms_output插件
    2、A库下查询gms_output插件。
Expect      :
    1、成功
    2、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()

    def test_gms_output(self):
        self.log.info(f'----{os.path.basename(__file__)} start----')
        text = '--step1:创建和node.yml同名且兼容A库的数据库;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.user_node.db_name};" \
                  f"create database {self.user_node.db_name} " \
                  f"dbcompatibility ='A';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res, '执行失败' + text)

        text = '--step2:A库下创建、查询gms_output插件;expect:创建成功，查询成功--'
        self.log.info(text)
        sql_cmd = f'''create extension gms_output;
                      select extname from pg_extension where extname ='gms_output';
                   '''
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname=self.user_node.db_name)
        self.log.info(sql_res)
        self.assertIn(self.constant.create_extension_success, sql_res, '执行失败' + text)
        self.assertEqual('gms_output', sql_res.splitlines()[-2].strip(), '执行失败' + text)

    def tearDown(self):
        self.log.info('--无须清理环境，待sql用例执行完后清理--')
        self.log.info(f'----{os.path.basename(__file__)} end-----')
 
